﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

	/*
	 * coded by moka 2009 / 07/ 23 
	 * name : moka's swing 
	 * blog : http://designplusplus.blogspot.com
	 */
	
	public class Main extends Sprite 
	{		
		private var bg:Bitmap;
		private var scene:Scene3D;
		private var viewport:Viewport3D;
		private var camera:Camera3D;
		private var render:BasicRenderEngine;
		private var plane:Plane;
		private var plane2:Plane;
		private var segH:Number;
		private var segW:Number;
		private var phase:Number;
		private var waveIntensity:Number;

		public function Main():void 
		{				
			// stage setting
			stage.scaleMode = "noScale";
			
			// background
			bg = addChild(new Bitmap(new BitmapData(640, 480, false, 0x000000)))as Bitmap;
			
			// pv3d essential setting
			scene = new Scene3D();
			camera = new Camera3D();
			viewport = new Viewport3D();
			render = new BasicRenderEngine();
			
			// segements of plane
			segH = 20;
			segW = 10;
			
			// width and height of plane
			var pw:Number = 2600;
			var ph:Number = 1500;
			
			
			// create texture
			var image:BitmapData = new BitmapData(50, 10, false);
			image.fillRect(new Rectangle(0, 0, 10, 10), 0x004358);
			image.fillRect(new Rectangle(10, 0, 10, 10), 0x1F8A70);
			image.fillRect(new Rectangle(20, 0, 10, 10), 0xBEDB39);
			image.fillRect(new Rectangle(30, 0, 10, 10), 0xFFE11A);
			image.fillRect(new Rectangle(40, 0, 10, 10), 0xFD7400);
			var texture:Shape = new Shape();
			texture.graphics.beginBitmapFill(image); // repeat x & y
			texture.graphics.drawRect(0,0,pw / 10, ph / 10);
			texture.graphics.endFill();			
			var mat:MovieMaterial = new MovieMaterial(texture, false, false, true);
			mat.doubleSided = true;
			
			// create plane
			plane = scene.addChild(new Plane(mat, pw, ph, segW, segH)) as Plane;
			plane2 = new Plane(null, pw, ph, segW, segH); // x/y coordinate reference
			
			// swing phase & swing intensity
			phase = 0;
			waveIntensity = 80;

			// Moka's work 2009
			var moka:TextField = addChild(new TextField()) as TextField
			moka.width = 300;
			moka.x = 20;
			moka.y = 200;
			moka.htmlText = "<font color='#FFFFFF' size='50'>Moka's work</font>";
			
			//event registration (事件註冊)
			stage.addEventListener(Event.ENTER_FRAME, onProcess)
		}		

		private function onProcess(e:Event):void {
			bg.bitmapData.fillRect(bg.bitmapData.rect, 0x000000);
			
			phase += 5;
			
			// swing the vertices
			for (var i:Number = 0; i < plane.geometry.vertices.length; i++ )
				plane.geometry.vertices[i].x = plane2.geometry.vertices[i].x + waveIntensity * Math.cos((phase + (i % segH) * (360 / segH)) / 180 * Math.PI);
			
			render.renderScene(scene, camera, viewport);
			bg.bitmapData.draw(viewport);
		}
	}		
}